﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GUI_Pages_ParentRegistration2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime myDateTime = DateTime.Now;
        string year = myDateTime.Year.ToString();
        int yr = int.Parse(year);

        ArrayList ayear = new ArrayList();
        int i;
        for (i = 1990; i <= yr; i++)
        {
            String n = i.ToString();
            ayear.Add(n);

        }

        ch1year.DataSource = ayear;
        ch1year.DataBind();

        if (IsPostBack)
        {
            addAnother.Visible = true;
        }

    }



    protected DateTime getBirthDate(String d, String m, String y)
    {
        Int32 day = Int32.Parse(d);
        Int32 month = Int32.Parse(m);
        Int32 year = Int32.Parse(y);
        DateTime dt = new DateTime(year, month, day);
        return dt;
    }



    ArrayList lang = new ArrayList();
    protected void ch1lang_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (ListItem li in ch1lang.Items)
        {
            if (li.Selected == true)
            {
                int x = Int32.Parse(ch1lang.SelectedValue);
                lang.Add(x);
            }
        }
              

    }


    int childID;

    protected void Button1_Click(object sender, EventArgs e)
    {
        string connStr = ConfigurationManager.ConnectionStrings["CSConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        SqlCommand cmd1 = new SqlCommand("ParentRegistration2Child", conn);
        cmd1.CommandType = CommandType.StoredProcedure;

        int acctID = (int)(Session["accID"]);

        cmd1.Parameters.Add(new SqlParameter("@acctID", acctID));
        cmd1.Parameters.Add(new SqlParameter("@child_name", ch1name.Text));
        cmd1.Parameters.Add(new SqlParameter("@DoB", getBirthDate(ch1day.SelectedValue, ch1month.SelectedValue, ch1year.SelectedValue)));
        cmd1.Parameters.Add(new SqlParameter("@gender", Convert.ToChar(ch1gen.SelectedValue)));
        cmd1.Parameters.Add(new SqlParameter("@schools", TextBox2.Text));
        cmd1.Parameters.Add(new SqlParameter("@grade", ch1grd.SelectedValue));
        cmd1.Parameters.Add(new SqlParameter("@health_conditions", TextBox1.Text));

        conn.Open();
        childID = Convert.ToInt32((decimal)cmd1.ExecuteScalar());
        conn.Close();

        object[] langArr = lang.ToArray();
        for (int k = 0; k < langArr.Length; k++)
        {
            int a = (int)langArr[k];
            SqlCommand cmd2 = new SqlCommand("ParentRegistration2ChildLanguages", conn);
            cmd2.CommandType = CommandType.StoredProcedure;

            cmd2.Parameters.Add(new SqlParameter("@childID", childID));
            cmd2.Parameters.Add(new SqlParameter("@languageID", a));

            conn.Open();
            cmd2.ExecuteNonQuery();
            conn.Close();
        }


    }



    protected void parentsform2_Click(object sender, EventArgs e)
    {
        int acctID = (int)(Session["accID"]);

        string connStr = ConfigurationManager.ConnectionStrings["CSConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        SqlCommand cmd1 = new SqlCommand("ParentRegistration2Child", conn);
        cmd1.CommandType = CommandType.StoredProcedure;

        cmd1.Parameters.Add(new SqlParameter("@acctID", acctID));
        cmd1.Parameters.Add(new SqlParameter("@child_name", ch1name.Text));
        cmd1.Parameters.Add(new SqlParameter("@DoB", getBirthDate(ch1day.SelectedValue, ch1month.SelectedValue, ch1year.SelectedValue)));
        cmd1.Parameters.Add(new SqlParameter("@gender", Convert.ToChar(ch1gen.SelectedValue)));
        cmd1.Parameters.Add(new SqlParameter("@schools", TextBox2.Text));
        cmd1.Parameters.Add(new SqlParameter("@grade", ch1grd.SelectedValue));
        cmd1.Parameters.Add(new SqlParameter("@health_conditions", TextBox1.Text));

        conn.Open();
        childID = Convert.ToInt32((decimal)cmd1.ExecuteScalar());
        conn.Close();

        object[] langArr = lang.ToArray();
        for (int k = 0; k < langArr.Length; k++)
        {
            int a = (int)langArr[k];
            SqlCommand cmd2 = new SqlCommand("ParentRegistration2ChildLanguages", conn);
            cmd2.CommandType = CommandType.StoredProcedure;

            cmd2.Parameters.Add(new SqlParameter("@childID", childID));
            cmd2.Parameters.Add(new SqlParameter("@languageID", a));

            conn.Open();
            cmd2.ExecuteNonQuery();
            conn.Close();
        }

        
        helper(acctID, ecR, ecRmob, ecRhome, ecRrel, e);
        helper(acctID, ecD, ecDmob, ecDaddr, TextBox3, e);

        Response.Redirect("http://www.cairositters.com/GUI/Pages/ParentRegistration3.aspx");
    }

    protected void helper(int a, TextBox t1, TextBox t2, TextBox t3, TextBox t4, EventArgs e)
    {
        string connStr = ConfigurationManager.ConnectionStrings["CSConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        SqlCommand cmd3 = new SqlCommand("Registration_EmergencyContacts", conn);
        cmd3.CommandType = CommandType.StoredProcedure;

        cmd3.Parameters.Add(new SqlParameter("@ec_name", t1.Text));
        cmd3.Parameters.Add(new SqlParameter("@ec_mobile_num", t2.Text));
        cmd3.Parameters.Add(new SqlParameter("@ec_phone_num", t3.Text));

        conn.Open();
        int ecID = Convert.ToInt32((decimal)cmd3.ExecuteScalar());
        conn.Close();

        SqlCommand cmd5 = new SqlCommand("Registration_EmergencyContacts_A", conn);
        cmd5.CommandType = CommandType.StoredProcedure;

        cmd5.Parameters.Add(new SqlParameter("@acctID", a));
        cmd5.Parameters.Add(new SqlParameter("@ecID", ecID));
        cmd5.Parameters.Add(new SqlParameter("@relationship", t4.Text));

        conn.Open();
        cmd5.ExecuteNonQuery();
        conn.Close();
    }

}